package captcha

import (
	"github.com/mojocn/base64Captcha"
)

// NewCaptcha 生成一个图形验证码
func NewCaptcha() (idKey string, base64 string, err error) {
	//字符,公式,验证码配置
	var configC = base64Captcha.ConfigCharacter{
		Height: 60,
		Width:  240,
		//const CaptchaModeNumber:数字,CaptchaModeAlphabet:字母,CaptchaModeArithmetic:算术,CaptchaModeNumberAlphabet:数字字母混合.
		Mode:               base64Captcha.CaptchaModeNumberAlphabet,
		ComplexOfNoiseText: base64Captcha.CaptchaComplexLower,
		ComplexOfNoiseDot:  base64Captcha.CaptchaComplexLower,
		IsShowHollowLine:   false,
		IsShowNoiseDot:     false,
		IsShowNoiseText:    false,
		IsShowSlimeLine:    false,
		IsShowSineLine:     false,
		CaptchaLen:         6,
	}

	//创建字符公式验证码
	//GenerateCaptcha 第一个参数为空字符串,包会自动在服务器一个随机种子给你产生随机uiid.
	idKey, cap := base64Captcha.GenerateCaptcha("", configC)

	//以base64编码
	base64 = base64Captcha.CaptchaWriteToBase64Encoding(cap)

	// 存入redis
	if e := set(idKey); e != nil {
		err = e
		return
	}
	return
}

// VerifyCaptcha 认证验证码·
func VerifyCaptcha(idKey, verifyValue string) bool {
	has, e := get(idKey)
	_ = del(idKey)
	if e != nil || has == false {
		return false
	}
	verifyResult := base64Captcha.VerifyCaptcha(idKey, verifyValue)
	return verifyResult
}
